/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package JavaClass;

import generate.lessonList.LessonList;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author dienvm60347
 */
public class LessonListSAXParser extends DefaultHandler{
    private List lessonlist = new ArrayList();
    private LessonList lesson = null;
    private boolean lessonid = false;
    private boolean lessonname = false;
    private boolean optionid = false;
    private String optid;

    public LessonListSAXParser(String optid) {
        this.optid = optid;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        if(qName.equals("lessonList")){
            lesson = new LessonList();
        }
        if(qName.equals("lessonId")){
            lessonid = true;
        }
        if(qName.equals("lessonName")){
            lessonname = true;
        }
        if(qName.equals("optionId")){
            optionid = true;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);
        String str = new String(ch, start, length);
        str = str.trim();
        
        if(optionid){
            BigInteger id = new BigInteger(str);
            lesson.setOptionId(id);
            optionid = false;
        }
        if(lessonid){
            BigInteger id = new BigInteger(str);
            lesson.setLessonId(id);
            lessonid = false;
        }
        if(lessonname){
            lesson.setLessonName(str);
            lessonname = false;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
        if(qName.equals("lessonList")){
            if(lesson!=null){
                BigInteger id = new BigInteger(optid);
                if(lesson.getOptionId().equals(id)){
                    lessonlist.add(lesson);
                }
            }
            lesson = null;
        }
    }
    public List getLessonlist() {
        return lessonlist;
    }
}
